﻿using Sys.Workflow.Engine.dynamic;
using Sys.Workflow.Engine.impl.runtime;
using Sys.Workflow.Engine.Impl.Interceptor;
using Sys.Workflow.Engine.Impl.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sys.Workflow.Engine.impl.cmd
{
    public class ChangeActivityStateCmd : ICommand<object>
    {
        protected ChangeActivityStateBuilderImpl changeActivityStateBuilder;

        public ChangeActivityStateCmd(ChangeActivityStateBuilderImpl changeActivityStateBuilder)
        {
            this.changeActivityStateBuilder = changeActivityStateBuilder;
        }


        public object Execute(ICommandContext commandContext)
        {
            if (changeActivityStateBuilder.GetMoveExecutionIdList().Count == 0 && changeActivityStateBuilder.GetMoveActivityIdList().Count == 0
                    && changeActivityStateBuilder.GetEnableActivityIdList().Count == 0)
            {

                throw new ActivitiIllegalArgumentException("No move execution or activity ids or enable activity ids provided");

            }
            else if ((changeActivityStateBuilder.GetMoveActivityIdList().Count() > 0 || changeActivityStateBuilder.GetEnableActivityIdList().Count > 0) && changeActivityStateBuilder.GetProcessInstanceId() == null)
            {
                throw new ActivitiIllegalArgumentException("Process instance id is required");
            }

            IDynamicStateManager dynamicStateManager = commandContext.ProcessEngineConfiguration.DynamicStateManager;
            dynamicStateManager.MoveExecutionState(changeActivityStateBuilder, commandContext);

            return null;
        }
    }
}
